# coding=utf-8
import time

import traceback
from monthdelta import monthdelta
import datetime
import requests
from pymongo import MongoClient
import com.local.hmh.Utils as Utils
import sys
from pandas import DataFrame
import pandas as pd
reload(sys)
sys.setdefaultencoding('utf-8')


def get_koufei_profit_series_from_tonghuashun(stock_code):
    file_url = 'http://basic.10jqka.com.cn/api/stock/export.php?type=report&code=' + stock_code + '&export=main'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36'+stock_code+' (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
    }
    response = requests.request("get", file_url, headers=headers)
    content = response.content
    try:
        path = Utils.save_finance_data(stock_code, 'tmp', content)
        xls_file = pd.read_excel(path, headers=headers, index_col=0, skiprows=[0], encoding='utf-8')
        Utils.remove_file(path)
        index_of_koufei = list(xls_file.index.values).index('扣非净利润(元)')
        series = xls_file.iloc[index_of_koufei]
        series.index = map(lambda period: period.replace("-", ""), series.index)
        return series
    except Exception as e:
        return None



'''
for i in range(0, 3000):
    stock_code = str(i).zfill(6)
    print "try to get profit data " + stock_code
    try:
        time.sleep(3)
        series = get_koufei_profit_series_from_tohuashun(stock_code)
        if series is not None:
            print 'success to get profit data ' + stock_code + "-->" + str(series.get('2018-03-31'))

        else:
            print "fail to get stock data" + stock_code

    except Exception as e:
        traceback.print_exc()
        print "fail to get profilt data " + stock_code

'''

def strip_finance_item_name(name, stock_code, type):

    name = name.encode("utf-8")

    if name == '归属于母公司股东的权益' or name == '归属于母公司的股东权益合计' or name == '归属于母公司所有者权益合计':
        return '归属于母公司股东权益合计'

    if name == '所有者权益合计' or name == '所有者权益(或股东权益)合计':
        return '股东权益合计'

    if name == '负债和所有者权益(或股东权益)总计':
        return '负债及股东权益总计'

    if name == '归属于母公司的净利润' or name == '归属于母公司所有者的净利润':
        return '归属于母公司股东的净利润'

    ###招商银行，利润表和资产负债表里面都有少数股东权益， 利润表里面应该是少数股东的损益
    if type == 'profit_statement' and name == '少数股东权益':
        name = '少数股东损益'

    if name == '实收资本(或股本)':
        return '股本'

    prefixes = ['其中：', '其中:', '减：', '减:', '加：', '加:',  '一、',  '二、', '三、' ,'四、', '五、', '六、', '七、', '八、', '九、' ]
    for i in range(len(prefixes)):
        prefix = prefixes[i]
        startwith = name.startswith(prefix);
        if startwith:
            name = name[len(prefix):];
    '''
    suffixes = ['(元/股)', '（减：减少）', '(减:减少)', '(减:增加)']
    for i in range(len(suffixes)):
        suffix = suffixes[i]
        index = name.find(suffix);
        if index != -1:
            name = name[0:index];
    '''
    replaces = ['(元/股)', '（减：减少）', '(减:减少)', '(减:增加)']
    for i in range(len(replaces)):
        rep = replaces[i]
        name = name.replace(rep, "")
    return name


name = strip_finance_item_name(u'负债和所有者权益(或股东权益)总计', '600009', 'balance_sheet')
print name



def download_and_save2(stock_code, url, type):

    def strip_item_name(name):
        return strip_finance_item_name(name, stock_code, type)
    try:
        dataFrame = pd.read_csv(url, sep='\t', index_col=0, encoding='gbk')
    except Exception as es:
        Utils.logger.warning("fail to download, code: " + stock_code + ", type: " + type + ", url:" + url)
        return
    dataFrame = dataFrame.drop([u'19700101', dataFrame.columns[-1]], axis=1)
    dataFrame = dataFrame.drop([u'单位'], axis=0)
    dataFrame = dataFrame.fillna(0)
    dataFrame = dataFrame.applymap(lambda x: float(x))
    dataFrame.index = map(strip_item_name, dataFrame.index)

    if type == 'profit_statement':
        dataFrame = dataFrame.transpose()
        koufei_series = get_koufei_profit_series_from_tonghuashun(stock_code)
        dataFrame[unicode('扣非净利润')] = koufei_series
        dataFrame = dataFrame.transpose()

    item_names = list(dataFrame.index)
    item_names.insert(0, unicode('报表日期'))

    for cname in dataFrame.columns:
        period = cname
        series = dataFrame[cname];
        data = series.to_dict();
        data[unicode('code')] = stock_code
        data[unicode('报表日期')] = period
        print "period ->" + period



get_koufei_profit_series_from_tonghuashun('000423')

#stock_code = '600009'
#url = "http://money.finance.sina.com.cn/corp/go.php/vDOWN_BalanceSheet/displaytype/4/stockid/" + "600009" + "/ctrl/all.phtml"
#profit_statement_url = "http://money.finance.sina.com.cn/corp/go.php/vDOWN_ProfitStatement/displaytype/4/stockid/" + "600009" + "/ctrl/all.phtml"
#download_and_save2(stock_code, profit_statement_url, "profit_statement")

def df_add_column():
    df = pd.DataFrame([[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]],
                      index=['2018-03', '2018-06', '2018-09', '2018-12'],
                      columns=['存货', '收入', '成本'])
    #series = pd.Series([9, 8, 7, 1], index=['2018-03', '2018-06', '2018-09', '2018-12'])
    series = pd.Series([], index=[])

    print df
    df.insert(1, 'profit', series)
    print df

df_add_column()